package com.ziqing.apeletnews.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ziqing.apeletnews.R;
import com.ziqing.apeletnews.adapter.CityAdapter;
import com.ziqing.apeletnews.base.BaseActivity;
import com.ziqing.apeletnews.baseMvp.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by YYJ on 2018/4/27.
 */

public class EditPersonalCenterActivity extends BaseActivity {
    @BindView(R.id.iv_avatar)
    RoundedImageView ivAvatar;
    @BindView(R.id.iv_post_avatar)
    RoundedImageView ivPostAvatar;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.ll_enter_name)
    LinearLayout llEnterName;
    @BindView(R.id.ll_enter_job)
    LinearLayout llEnterJob;
    @BindView(R.id.ll_enter_content)
    LinearLayout llEnterContent;
    @BindView(R.id.ll_enter_address)
    LinearLayout llEnterAddress;
    @BindView(R.id.ll_enter_sex)
    LinearLayout llEnterSex;
    @BindView(R.id.ll_enter_invite)
    LinearLayout llEnterInvite;

    private int REQUEST_CITY = 0x01;
    CityPickerView mPicker = new CityPickerView();
    StringBuffer address = new StringBuffer();
    private List<String> data = new ArrayList<>();
    private CityAdapter adapter;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_personal_center);
        ButterKnife.bind(this);
        setTitle("编辑个人中心");
        setUpToolbar(R.drawable.icon_back);
        /**
         * 预先加载仿iOS滚轮实现的全部数据
         */
        mPicker.init(this);
    }

    @OnClick({R.id.ll_enter_name, R.id.ll_enter_job, R.id.ll_enter_content, R.id.ll_enter_address, R.id.ll_enter_sex, R.id.ll_enter_invite, R.id.iv_post_avatar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_enter_name:
                startActivity(new Intent(this, EditNameActivity.class));
                break;
            case R.id.ll_enter_job:
                startActivity(new Intent(this, EditJobActivity.class));
                break;
            case R.id.ll_enter_content:
                startActivity(new Intent(this, EditIntroductionActivity.class));
                break;
            case R.id.ll_enter_address:
                initData();
                break;
            case R.id.ll_enter_sex:
                Dialog dialogSex = new Dialog(this, R.style.dialog);
                View viewSex = getLayoutInflater().inflate(R.layout.dialog_sex_select, null);
                dialogSex.setContentView(viewSex, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Window window = dialogSex.getWindow();
                // 可以在此设置显示动画
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.x = 0;
                wl.y = getWindowManager().getDefaultDisplay().getHeight();
                // 以下这两句是为了保证按钮可以水平满屏
                wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
                wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                // 设置显示位置
                dialogSex.onWindowAttributesChanged(wl);
                dialogSex.show();
                LinearLayout ll_man = viewSex.findViewById(R.id.ll_man);
                LinearLayout ll_woman = viewSex.findViewById(R.id.ll_woman);
                ll_man.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvSex.setText("男");
                        dialogSex.dismiss();
                    }
                });
                ll_woman.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvSex.setText("女");
                        dialogSex.dismiss();
                    }
                });
                break;
            case R.id.ll_enter_invite:
                startActivity(new Intent(this,InviteFriendActivity.class));
                break;
        }

    }

    private void initData() {
        //添加默认的配置，不需要自己定义
        CityConfig cityConfig = new CityConfig.Builder()
                .setCityWheelType(CityConfig.WheelType.PRO_CITY)
                .build();
        mPicker.setConfig(cityConfig);

        //监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {

                //省份
                if (province != null) {
                    address.append(province.getName() + "  ");
                }

                //城市
                if (city != null) {
                    address.append(city.getName());
                }

                //地区
                if (district != null) {

                }
                tvAddress.setText(address.toString());
                address = new StringBuffer();

            }

            @Override
            public void onCancel() {
//                ToastUtils.showLongToast(getApplicationContext(), "已取消");
            }
        });

        //显示
        mPicker.showCityPicker();
    }


}
